<template>
  <div>
    <div class="add-container" v-if="this.userInfo.username">
      <div class="add-box">
        <Form :model="formData" ref="formData" label-position="left" :label-width="100" :rules="ruleInline">
          <FormItem label="头像" >
           <img width=200 :src="formData.atrr" alt="" @click="atrrClick(formData.atrr)">
          </FormItem>
          <FormItem label="用户名" prop="username" width='600'>
            <i-input v-model="formData.username" size="large"></i-input>
          </FormItem>
          <FormItem label="密码" prop="password">
            <i-input v-model="formData.password" size="large"></i-input>
          </FormItem>
          <FormItem label="邮箱" prop="mail">
            <i-input v-model="formData.mail" size="large"></i-input>
          </FormItem>
          <FormItem label="手机号码" prop="phone">
            <i-input v-model="formData.phone" size="large"></i-input>
          </FormItem>

        </Form>
      </div>
      <div class="add-submit">
        <Button type="primary" @click="add('formData')">保存修改</Button>
      </div>
    </div>
     <Modal v-model="attrflag" width="600">
        <p slot="header">
          <Icon type="edit"></Icon>
          <span>选择头像</span>
        </p>
        <div>
          <div class="atrrbox">
            <div class="atrr-item" v-for="(item,index) in atrrlist" :key="index" :class="atrrindex===index?'active':''">
              <img @click="atrrClick(item)" :src="item" width="150" alt="">
            </div>
          </div>
        </div>
        <div slot="footer">
            <Button type="primary" size="large" long @click="editAction">确定</Button>
        </div>
    </Modal>
  </div>
</template>

<script>
import Distpicker from 'v-distpicker';
import { mapMutations, mapState } from 'vuex';
export default {
  name: 'MyIndex',
  data () {
    return {
      formData: {
        username: '',
        password: '',
        mail: '',
        phone: '',
        atrr: ''
      },
      atrrindex: 0,
      attrflag: false,
      atrrlist: [
        'static/img/atrr/1.png',
        'static/img/atrr/2.png',
        'static/img/atrr/3.png',
        'static/img/atrr/4.png',
        'static/img/atrr/5.png',
        'static/img/atrr/6.png'
      ],
      ruleInline: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mail: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '邮箱格式错误', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { type: 'string', min: 6, message: '密码长度不能小于6', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { type: 'string', pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '手机号格式出错', trigger: 'blur' }
        ]
      },
      list: []
    };
  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    ...mapMutations(['SET_USER_LOGIN_INFO']),
    atrrClick (i) {
      this.attrflag = true;
      const index = this.atrrlist.indexOf(i);
      this.atrrindex = index;
    },
    editAction () {
      this.attrflag = false;
      this.formData.atrr = this.atrrlist[this.atrrindex];
    },
    getCity (data) {
      this.formData.city = data.value;
    },
    getArea (data) {
      this.formData.area = data.value;
    },
    add (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.$Message.success('修改成功');
          this.SET_USER_LOGIN_INFO(this.formData);
          localStorage.setItem('login-info', JSON.stringify(this.formData));
        }
      });
    }
  },
  created () {
    this.formData = JSON.parse(localStorage.getItem('login-info'));
    // let _this = this;
    // 根据自己需要来监听对应的key
  },
  components: {
    Distpicker
  }
};
</script>

<style scoped>
.add-container {
  margin: 15px auto;
  width: 50%;
  font-size:30px;
  min-width: 600px;
}
.add-title {
  margin-bottom: 15px;
  text-align: center;
}
.add-submit {
  display: flex;
  justify-content: center;
}
.atrr-item {
  display:inline-block;
  margin:10px;
  padding : 5px;
  border: 2px solid #fff ;
}
.atrr-item.active {
  border: 2px solid red ;

}
</style>
